<?php
/**
 *
 *
 * @category     Shuxun Cms
 * @date         2015年10月19日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         function.php
 */
namespace  Scshux\Tag;
use Phalcon\Di\FactoryDefault;
use Scshux\Utils\JSON;
use Scshux\Utils\Image;
class SmartyTag
{

	/**
	 * url处理
	 * @param  $args
	 */
	public  static  function  url($args=array(),&$smarty)
	{
		$di = FactoryDefault::getDefault();
		if(empty($args['path'])){
			return $di->get('url')->get('');
		}
		$path = $args['path'];
		unset($args['path']);
	    return $di->get('url')->get($path,$args);
	}

	/**
	 * json格式化
	 * @param  $args
	 * @param  $smarty
	 */
	public static  function  json($args=array(),&$smarty)
	{
		return  JSON::encode($args['data']);
	}

	/**
	 * 翻译
	 * @param  $args
	 * @param  $smarty
	 */
	public  static  function t($args=array(),&$smarty)
	{

	}


	/**
	 * 图片处理
	 * @param  $args
	 * @param  $smarty
	 */
	public static  function  img($args=array(),&$smarty)
	{
		$width  = empty($args['w'])?50:intval($args['w']);
		$height = empty($args['h'])?50:intval($args['h']);
		$pic    = empty($args['pic'])?'/skin/default.jpg':$args['pic'];
		$thumbpath    = dirname(APPLICATION_PATH).'/media/cache/'.$width.'_'.$height.'/';
		if(!file_exists($thumbpath)){
			mkdir($thumbpath,0777,true);
		}

		$pic = dirname(APPLICATION_PATH).$pic;
		if(!file_exists($pic)){
			$pic =  dirname(APPLICATION_PATH).'/skin/default.jpg';
		}
		$thumb = $thumbpath .basename($pic);
		$t=new Image();
		$t->watermark=false;
		$t->make_thumb($pic,$thumb,$width,$height);
		unset($t);
		return str_replace(dirname(APPLICATION_PATH), '', $thumb);

	}

}